package component.edittext;
import component.button.MaterialButton;
import component.util.MaterialColors;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

/**
  * @Description 大规模文本编辑及发送
  * @author 孔明浩
  * @Date 2020/3/31
  */
public class InputArea extends JPanel {
    private boolean enableFileChooseButton = true;
    private JScrollPane scrollPane = new JScrollPane();
    private JTextArea inputArea = new JTextArea();
    //开头横向盒子
    private Box hBox = Box.createHorizontalBox();
    private JButton endButton = new JButton();
    private JComponent fileChooseButton = MaterialButton.BlueButton("",null);

    public InputArea(Box hBox, JButton endButton, JComponent fileChooseButton){
        super();
        if (hBox != null){
            this.hBox = hBox;
        }
        if (endButton != null){
            this.endButton = endButton;
        }
        if (fileChooseButton != null){
            this.fileChooseButton = fileChooseButton;
        } else {
            this.enableFileChooseButton = false;
        }
        initial();
    }

    protected void initial(){
        GridBagLayout gridBagLayout = new GridBagLayout();
        GridBagConstraints gridBagConstraints;

        //滚动输入文本框布局
        this.setBackground(MaterialColors.WHITE);
        this.setLayout(gridBagLayout);
        inputArea.setLineWrap(true);
        inputArea.setBackground(MaterialColors.WHITE);
        scrollPane = new JScrollPane(inputArea);
        scrollPane.setBorder(new LineBorder(MaterialColors.WHITE,12));

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.CENTER;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagLayout.addLayoutComponent(scrollPane, gridBagConstraints);

        //文件选择按钮
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new Insets(0,16,16,16);
        gridBagLayout.addLayoutComponent(fileChooseButton,gridBagConstraints);

        //底部按钮
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.SOUTHEAST;
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new Insets(0,16,16,16);
        gridBagLayout.addLayoutComponent(endButton, gridBagConstraints);

        this.add(hBox);
        this.add(scrollPane);
        this.add(fileChooseButton);
        this.add(endButton);

        this.fileChooseButton.setVisible(enableFileChooseButton);
    }

    public String getText(){
        return this.inputArea.getText();
    }

    public void setText(String text){
        this.inputArea.setText(text);
        repaint();
    }

    public JTextArea getInputArea() {
        return inputArea;
    }

    public void setEditable(boolean b){
        this.inputArea.setEditable(b);
    }

    public void setInputAreaFont(Font font){
        this.inputArea.setFont(font);
    }
}
